package com.ms.p2p.producter.impl;

import com.ms.p2p.bean.ReturnSms;
import com.ms.p2p.producter.Producter;
import com.ms.p2p.queen.Email;
import com.ms.p2p.queen.SMS;
import com.ms.p2p.service.impl.SendServiceImpl;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;

import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;

/**
 * Created by daimajia on 2015/5/12.
 */
@Service("producer")
public class ProducterImpl implements  Producter{

    @Value("${mail.username}")
    private String fromMailUsername;
    @Resource
    private MailSender mailSender;
    @Autowired
    private RestTemplate restTemplate;
    protected static Logger logger = Logger.getLogger(SendServiceImpl.class);



    public void SendMail(Email email) {
       // jmsTemplate.convertAndSend(mail);
        try {
            SimpleMailMessage mailMessage = new SimpleMailMessage();
            mailMessage.setFrom(fromMailUsername);
            mailMessage.setTo(email.getTo());
            mailMessage.setSubject(email.getSubject());
            mailMessage.setText(email.getContent());
            mailSender.send(mailMessage);
        } catch (Exception e) {
            logger.error("邮件发送失败", e);
            e.printStackTrace();
        }
    }

    public void sendSMS(SMS sms)


    {
        ReturnSms returnSms = restTemplate.getForEntity(sms.getUrl().toString(), ReturnSms.class).getBody();
        if (returnSms != null && "success".equals(returnSms.getReturnstatus().toLowerCase())) {
            System.out.print("发送短信成功");
        }else{
            System.out.print("发送短信失败");
        }
       // jmsTemplate.convertAndSend(sms);
    }

}
